import pgzrun
import json
import tkinter as tk
import _thread
import os
WIDTH=800
HEIGHT=800
board=[[0,0,0,0],
       [0,0,0,0],
       [0,0,0,0],
       [0,0,0,0],
       [0,0,0,0]]
name="BinLinCaoYing.json"
p="/home/pi/py_class_1/whq/else/three_country_slide_puzzle/"
def load():
       global board
       ftext=0
       with open(file = p+name, mode = 'r', encoding = 'utf8') as f: #读入文档
              ftext = f.read()
       data = json.loads(ftext)
       board=data
load()
Actor_list=[0 for x in range(11)]
clicked=-1
spstr=""
levels=[("ZhiHuiRuoDing","指挥若定"),
        ("QiTouBinJin","齐头并进"),
        ("JiangYongCaoYing","将涌曹营"),
        ("JiangShouJiaoLou","将守角楼"),
        ("HengDaoLiMa","横刀立马"),
        ("BinLinCaoYing","兵临曹营"),
        ("BinFenSanLu","兵分三路")]
def init():
       global Actor_list
       Actor_list=[0 for x in range(11)]
       spstr=""
       l=[0 for x in range(11)]
       for j in range(4):
              for i in range(5):
                     if((l[board[i][j]]==1) or board[i][j]==0):
                            continue
                     n=""
                     if(board[i][j]==1):
                            n="zhang"
                     elif(board[i][j]==2):
                            n="zhao"
                     elif(board[i][j]==3):
                            n="ma"
                     elif(board[i][j]==6):
                            n="huang"
                     elif(board[i][j]==4):
                            n="guan"
                     elif(board[i][j]==5):
                            n="cao"
                     else:
                            n="zu"
                     Actor_list[board[i][j]]=(Actor(n,topleft=(100+j*150,100+i*120)))
                     l[board[i][j]]=1
       Actor_list=Actor_list[1:]
       print(Actor_list)
init()

def check_win():
       global spstr
       if(board[3][1]==5 and board[3][2]==5 and board[4][1]==5 and board[4][2]==5):
              spstr="win"

def need(x):
       global name
       name=x
       load()
       init()

def tktk():
       global name
       root = tk.Tk()
       root.title('选关') 
       root.iconphoto(False, tk.PhotoImage(file=p+'images/头像.png'))
       for i in range(len(levels)):
              Button1 = tk.Button(root,text = levels[i][1],command = need(levels[i][0]+".json"))
              Button1.pack(side=tk.LEFT)
       root.mainloop()

def on_mouse_down(pos):
       global clicked
       x,y=(pos[1]-100)//120,(pos[0]-100)//150
       try:
              clicked=board[x][y]-1
       except:
              clicked=-1
def on_key_down(key):
       global clicked,board
       if(clicked==-1):
              return
       Ac=Actor_list[clicked]
       n=Ac.size
       w=n[1]//120
       h=n[0]//150
       x,y=(Ac.topleft[1]-100)//120,(Ac.topleft[0]-100)//150
       dir=(0,0)
       if(key==keys.W):
              dir=(0,-1)
       elif(key==keys.S):
              dir=(0,1)
       elif(key==keys.A):
              dir=(-1,0)
       elif(key==keys.D):
              dir=(1,0)
       xboard=[[board[i][j] for j in range(4)] for i in range(5)]
       try:
              for j in range(int(x),int(x+w)):
                     for i in range(int(y),int(y+h)):
                            if(i+dir[0]<0 or j+dir[1]<0):
                                   clicked=-1
                                   return
                            if(xboard[j+dir[1]][i+dir[0]]!=0 and xboard[j+dir[1]][i+dir[0]]!=clicked+1):
                                   clicked=-1
                                   return
                            xboard[j+dir[1]][i+dir[0]]+=clicked+1
                            xboard[j][i]-=clicked+1
       except:
              clicked=-1
              return
       board=xboard[:]
       animate(Ac,pos=(Ac.pos[0]+dir[0]*150,Ac.pos[1]+dir[1]*120),duration=0.1)
       clicked=-1

def update():
       check_win()
      
def draw():
       screen.clear()
       screen.blit("background",(0,0))
       for i in Actor_list:
              i.draw()
       if(spstr != ''):
              screen.draw.text(spstr, center=(400, 400), fontsize=120, color="red")
_thread.start_new_thread(tktk, ())
pgzrun.go()